using System;
using System.Collections.Generic;
using System.Text;
using TetraEngine;
using Microsoft.Xna.Framework;

namespace Tetrazune
{
    class GameOver
    {
        readonly TimeSpan waitTime = TimeSpan.FromMilliseconds(70);

        TetraGame tetra;
        TimeSpan rem = TimeSpan.Zero;
        int index = GameBoard.GameBoardRows - 1;
        Random r = new Random();
        bool flip = false;

        public GameOver(TetraGame tetra)
        {
            this.tetra = tetra;
        }

        public bool Finished
        {
            get { return index > GameBoard.GameBoardRows + 10; }
        }

        public bool ScreenWasFilled
        {
            get { return flip; }
        }

        public void Update(GameTime gameTime)
        {
            rem += gameTime.ElapsedGameTime;
            if (rem < waitTime) return;
            rem -= waitTime;

            if (Finished) return;

            if (flip)
                UpdateDown();
            else
                UpdateUp();
        }

        private void UpdateDown()
        {
            if (index >= GameBoard.GameBoardRows)
            {
                index++;
                return;
            }
            for (int i = 0; i < GameBoard.GameBoardColumns; i++)
                tetra.GameBoard[index, i] = 0;
            index++;
        }

        private void UpdateUp()
        {
            if (index < 0) return;
            for (int i = 0; i < GameBoard.GameBoardColumns; i++)
            {
                if (tetra.GameBoard[index, i] < 1)
                    tetra.GameBoard[index, i] = r.Next(1, 7);
            }
            index--;
            if (index <= 0) flip = true;
        }
    }
}